<?php
/**
 * 系统权限
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/8/11 0011
 * Time: 10:37
 */
namespace Admin\Controller;
use Common\Controller\AuthController;
use Think\Controller;

class AccessControlController extends AuthController
{

    //功能设置
    public function featureSet()
    {


        $arr = M('AuthRule')->where("status=%d",1)->select();
        $counts=count($arr);
        $list=oneL($arr);
        $page=page($counts,20,5);
        $_list=array_slice($list,$page->firstRow,$page->listRows);
        $this->assign('_list', $_list);
        $this->assign('_page', $page->show());
        $this->assign('title','系统功能');
        $this->display();
    }

    //新增功能
    public function add($id='')
    {

        if (IS_POST) {
            $id = null ? null : $_POST['id'];
            $arr = D('AuthRule')->ruleAdd($id, $_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info']);
            } else {
                $this->error($arr['info']);
            }
        } else {
            $cate = M('AuthRule')->where("status=%d", 1)->select();
            //编辑信息
            if (I('get.id')) {
                $edit_arr = D('AuthRule')->ruleList(I('get.id'));
                $this->assign('edit', $edit_arr);
//                p($edit_arr);
            }
            $this->assign('cate', oneL($cate));
//            p(oneL($cate));

            $this->assign('title','新增功能模块');
            $this->display();
        }
    }

    //删除功能模块
    public function mdel($id = '')
    {
        $result = M('AuthRule')->where("id=%d", $id)->delete();
        if ($result) {
            $this->success('删除成功', '', IS_AJAX);
        } else {
            $this->error('删除失败', '', IS_AJAX);
        }
    }

    //岗位列表
    public function stationList()
    {

        $arr = D('AuthGroup')->lister();
        $list=$arr['list'];
        foreach($list as &$v){
            $area=explode(',',$v['area_id']);
            foreach($area as $vo){
                $area_name=M('Area')->where("id=%d",$vo)->getField('area_name');
                $v['area_name'].=$area_name.',';

            }
            $v['area_name']=substr($v['area_name'],0,-1);
        }
        $this->assign('_list',$list);
        $this->assign('_page', $arr['page']->show());
        $this->assign('title','岗位列表');
        $this->display();
    }

    //新增岗位
    public function addStation()
    {
        if (IS_POST) {
            $_POST['rules'] = join(',', $_POST['rules']);
            $arr = D('AuthGroup')->changeGroup('', $_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info'],U('AccessControl/stationList'));
            } else {
                $this->error($arr['info']);
            }
        } else {
            $areaList=M('Area')->select();
            $this->assign('areaList',$areaList);
            $this->assign('node', manyL($this->node()));
//            p(manyL($this->node()));
            $this->assign('title','新增岗位');
            $this->display();
        }

    }

    //编辑岗位
    public function sedit($id = '')
    {

        if (IS_POST) {
            $_POST['rules'] = join(',', $_POST['rules']);
            $re = D('AuthGroup')->changeGroup($_POST['id'], $_POST);
            if ($re['state'] == 'success') {
                $this->success($re['info']);
            } else {
                $this->error($re['info']);
            }
        } else {

            $arr = D('AuthGroup')->lister($id);
            $rules=explode(',',$arr['rules']);//节点数组
            $node=$this->node();//所有节点
            foreach($node as &$v){
                if(in_array($v['id'],$rules)){
                    $v['check']=1;
                }else{
                    $v['check']=0;
                }
            }

            $areaList=M('Area')->select();
            $this->assign('areaList',$areaList);
            $this->assign('edit', $arr);
            $this->assign('node', manyL($node));
            $this->assign('title','编辑岗位');
            $this->display();
        }

    }

    private function node()
    {
        $cate = M('AuthRule')->where("status=%d", 1)->select();
        return $cate;
    }

    //删除岗位
    public function sdel($id = '')
    {
        if($id==8 || $id==9) {
            $this->error('不能删除审核岗位!', '', IS_AJAX);
            die;
        }
        $group_arr=M('AuthGroupAccess')->where("group_id=%d",$id)->find();
        if(!empty($group_arr)) {
            $this->error('请先删除该岗位下的用户!', '', IS_AJAX);
            die;
        }

        $re = M('AuthGroup')->where("id=%d", $id)->delete();
        if ($re) {
            $this->success('删除成功', '', IS_AJAX);
        } else {
            $this->error('删除失败', '', IS_AJAX);
        }
    }

    //用户列表
    public function userList()
    {
        $arr = D('Member')->lister();
        $glist=M('authGroup')->field("id,title")->select();
        $this->assign('glist',$glist);
        $this->assign('_list', $arr['list']);
        $this->assign('_page', $arr['page']->show());
        $this->assign('title','用户信息列表');
        $this->display();
    }

    //新增用户
    public function addUser()
    {

        if (IS_POST) {

            $arr = D('Member')->adduser('', $_POST);
            if ($arr['state'] == 'success') {
                $this->success($arr['info'],U('AccessControl/userList'));
            } else {
                $this->error($arr['info']);
            }
        } else {
            $list = M('AuthGroup')->where("status=%d", 1)->select();
            $this->assign('_list', $list);
            $this->assign('title','新增用户信息');
            $this->display();
        }

    }

    //用户状态
    public function userstate()
    {
        $id = $_GET['id'];
        $data = M('Member')->where("id=%d", $id)->save(['status' => $_GET['status']]);
        if ($data) {
            $this->success('操作成功', '', IS_AJAX);
        } else {
            $this->error('操作失败', '', IS_AJAX);
        }
    }

    //删除用户
    public function userdel($id = '')
    {
        if (M('Member')->where("id=%d", $id)->delete()) {
            M('GroupAccess')->where("uid=%d",$id)->delete();
            $this->success('删除成功', '', IS_AJAX);
        } else {
            $this->error('删除失败', '', IS_AJAX);
        }

    }


    //更新分组
    public function gupdate(){
       $data['group_id']=$_POST['group_id'];
       $data['uid']=$_POST['uid'];
        $arr=M('AuthGroupAccess')->where("uid=%d",$_POST['uid'])->save($data);
        $nick=M('Member')->where("id=%d",$_POST['uid'])->save(['nickname'=>$_POST['nick'],'phone_number'=>$_POST['phone_number']]);
        if($arr || $nick){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //访客记录
    public function visitor()
    {

        $uid=$_GET['id'];
        $userinfo=D('Member')->userDetail($uid);
        $re=D('Call')->pageList($uid);
        $this->assign('_list',$re['list']);
        $this->assign('_page',$re['page']->show());
        $this->assign('userinfo',$userinfo);
        $this->assign('title','拜访记录');
        $this->display();
    }
    //获取地理位置信息
    public function location(){
        if(IS_AJAX){
            $uid=$_POST['uid'];
            $gl_time=$_POST['gl_time'];
            $re=M('Location')->where("uid=%d and gl_time=%d",[$uid,$gl_time])->select();
            if($re){
                $arr['state']='success';
                $arr['info']=$re;
            }else{
                $arr['state']='error';
                $arr['info']='没有地理位置信息';
            }
            $this->ajaxReturn($arr);

        }
    }

}













